﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB.YJKControls;
using YJKRevitFloorModule.AutoSplitFloor;
using YJKRevitFloorModule.EnumDefine;

namespace YJKRevitFloorModule.Forms
{
	public partial class SplitFloorForm : System.Windows.Forms.Form
	{
		public FilterType TheFilterType
		{
			get
			{
				FilterType filterType = FilterType.FT_None;
				if (this.chkBeam.Checked)
				{
					filterType |= FilterType.FT_Beam;
				}
				if (this.chkWall.Checked)
				{
					filterType |= FilterType.FT_Wall;
				}
				if (this.chkGrid.Checked)
				{
					filterType |= FilterType.FT_Grid;
				}
				if (this.chkRefPlane.Checked)
				{
					filterType |= FilterType.FT_RefPlane;
				}
				return filterType;
			}
		}

		public SplitFloorForm()
		{
			this.InitializeComponent();
		}

		private void SearchFloorForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void SearchFloorForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SearchFloorForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private void SearchFloorForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "SplitFloorForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.chkBeam.Checked = true;
				this.chkWall.Checked = true;
				this.chkGrid.Checked = true;
				this.chkRefPlane.Checked = true;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.chkBeam.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkWall.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkGrid.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkRefPlane.Checked = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SplitFloorForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.chkBeam.Checked);
			streamWriter.WriteLine(this.chkWall.Checked);
			streamWriter.WriteLine(this.chkGrid.Checked);
			streamWriter.WriteLine(this.chkRefPlane.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			SplitFloorForm.SendMessage(CmdAutoSplitFloor._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SplitFloorForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SplitFloorForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}
	}
}
